package jwxx.server;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BattleAct {
 
	public boolean result;	
	public List<BattleExt> act(int id1,int id2) throws SQLException{
		List<BattleExt> list =new ArrayList<BattleExt>();
		SearchData search_play=new SearchData();
		PlayAttribute search_play1=search_play.count(id1);
		PlayAttribute search_play2=search_play.count(id2);	
		int dam_min1,dam_max1,metal1,wood1,water1,fire1,earth1,type1,hp1,att_elements_1 = 0,def_elements_1 = 0;	
		int dam_min2,dam_max2,metal2,wood2,water2,fire2,earth2,type2,hp2,att_elements_2 = 0,def_elements_2 = 0;	
		int valid_direct_hit_1,skill_probability_1;
		int valid_direct_hit_2,skill_probability_2;
		float valid_skill_plus_1,valid_skill_plus_2;
		hp1=search_play1.getCurrHp();
		hp2=search_play2.getCurrHp();
		metal1=search_play1.getPlayMetal();		
		wood1=search_play1.getPlayWood();
		water1=search_play1.getPlayWater();
		fire1=search_play1.getPlayFire();
		earth1=search_play1.getPlayEarth();
		valid_direct_hit_1=search_play1.getValidDirectHit();
		skill_probability_1=search_play1.getSkillProbability();		
		valid_skill_plus_1=search_play1.getSkillPlus();	
		dam_min1=search_play1.getValidDamMin();
		dam_max1=search_play1.getValidDamMax();
		type1=search_play1.getItemType();
		
		
		metal2=search_play2.getPlayMetal();		
		wood2=search_play2.getPlayWood();
		water2=search_play2.getPlayWater();
		fire2=search_play2.getPlayFire();
		earth2=search_play2.getPlayEarth();	
		valid_direct_hit_2=search_play2.getValidDirectHit();
		skill_probability_2=search_play2.getSkillProbability();
		valid_skill_plus_2=search_play2.getSkillPlus();
		dam_min2=search_play2.getValidDamMin();
		dam_max2=search_play2.getValidDamMax();		
		type2=search_play2.getItemType();
		
		if(type1==1)
		{
			att_elements_1=metal1;
			def_elements_2=wood2;
		}else if(type1==2)
		{
			att_elements_1=wood1;
			def_elements_2=earth2;
		}else if(type1==3)
		{
			att_elements_1=water1;	
			def_elements_2=fire2;
		}else if(type1==4)
		{
			att_elements_1=fire1;			
			def_elements_2=metal2;
		}else if(type1==5)
		{
			att_elements_1=earth1;	
			def_elements_2=water2;
		}
		if(type2==1)
		{
			att_elements_2=metal2;
			def_elements_1=wood1;
		}else if(type2==2)
		{
			att_elements_2=wood2;
			def_elements_1=earth1;
		}else if(type2==3)
		{
			att_elements_2=water2;	
			def_elements_1=fire1;
		}else if(type2==4)
		{
			att_elements_2=fire2;			
			def_elements_1=metal1;
		}else if(type2==5)
		{
			att_elements_2=earth2;	
			def_elements_1=water1;
		}		
		int index=0;	
		for(;(hp1>0)&&(hp2>0);)
		{
			int hp3,hp4,dam1,dam2;
			hp3= hp1;
			BattleExt battle_inf_1=new BattleExt();
			battle_inf_1.setIndex(index);
			battle_inf_1.setName(id1);
			if(GetRandom(100)<valid_direct_hit_1)
			{
				if(GetRandom(100)<skill_probability_1)
				{
					battle_inf_1.setAction(1);
					if(att_elements_1>=def_elements_2)
					{
						dam1=(int)(((float)dam_min1+(float)GetRandom(dam_max1-dam_min1))*(1+(2*((float)att_elements_1-(float)def_elements_2)/((float)att_elements_1+(float)def_elements_2)))*(1+valid_skill_plus_1));
					}else{
						dam1=(int)(((float)dam_min1+(float)GetRandom(dam_max1-dam_min1))*(1+valid_skill_plus_1)/2);
					}
				}else
				{
					battle_inf_1.setAction(0);
					if(att_elements_1>=def_elements_2)
					{
						dam1=(int)(((float)dam_min1+(float)GetRandom(dam_max1-dam_min1))*(1+(2*((float)att_elements_1-(float)def_elements_2)/((float)att_elements_1+(float)def_elements_2))));
					}else{
						dam1=(dam_min1+GetRandom(dam_max1-dam_min1))/2;
					}					
				}	
				
			}else{
				battle_inf_1.setAction(0);
				dam1=0;
			}
			battle_inf_1.setDam(dam1);
			hp4=hp2-dam1;
			index++;
			if(hp4<=0)
			{
				hp4=0;
				battle_inf_1.setDealhp(hp4);	
				setResult(true);
				list.add(battle_inf_1);
				break;
			}				
			battle_inf_1.setDealhp(hp4);
			list.add(battle_inf_1);
			
			
			BattleExt battle_inf_2=new BattleExt();						
			hp2=hp4;
			battle_inf_2.setIndex(index);
			battle_inf_2.setName(id2);			
			if(GetRandom(100)<valid_direct_hit_2)
			{
				if(GetRandom(100)<skill_probability_2)
				{
					battle_inf_2.setAction(1);
					if(att_elements_2>=def_elements_1)
					{
						dam2=(int)(((float)dam_min2+(float)GetRandom(dam_max2-dam_min2))*(1+(2*((float)att_elements_2-(float)def_elements_1)/((float)att_elements_2+(float)def_elements_1)))*(1+valid_skill_plus_2));
					}else{
						dam2=(int)(((float)dam_min2+GetRandom(dam_max2-dam_min2))*(1+valid_skill_plus_2)/2);
					}
				}else
				{
					battle_inf_2.setAction(0);
					if(att_elements_2>=def_elements_1)
					{
						dam2=(int)(((float)dam_min2+GetRandom(dam_max2-dam_min2))*(1+(2*((float)att_elements_2-(float)def_elements_1)/((float)att_elements_2+(float)def_elements_1))));
					}else{
						dam2=(dam_min2+GetRandom(dam_max2-dam_min2))/2;
					}					
				}	
				
			}else{
				battle_inf_2.setAction(0);
				dam2=0;
			}	
			battle_inf_2.setDam(dam2);
			hp1=hp3-dam2;
			index++;
			if(hp1<=0)
			{
				hp1=0;
				battle_inf_2.setDealhp(hp1);
				list.add(battle_inf_2);
				setResult(false);
				break;
			}	
			battle_inf_2.setDealhp(hp1);		
			list.add(battle_inf_2);
		}
		

		return list;
	}
	
public int GetRandom(int i){	
	Random random=new Random(); 
	return Math.abs(random.nextInt())%i;	
}

public void setResult(boolean value)
	{
	result = value;
	}

public boolean getResult(){
	return result;
}


}
